<?php


namespace app\index\model;


use think\Exception;
use think\Model;

class User extends Model
{
    public static $user_role_list=[1=>'管理',2=>'运营','3'=>'主播',4=>'仓库',5=>'物流商'];

    public static function getUserList()
    {
        $UserClass=new self();

        $user_list=[];

        $group_list=Group::getGroupList();

        $data=$UserClass->where('status', 1)->where('del_flag', 0)->field('user_name,group_id,id')->select()->toArray();
        foreach ($data as $key=>$value){
            if($value['group_id']) {
                $user_list[$value['group_id']][] = $value;
            }else{
                $user_list[0][]=$value;
            }
        }

        return ['user_list'=>$user_list,'group_list'=>$group_list];
    }

    public static function getUserIdListByGroup($group){
        $UserClass=new self();

        $data=$UserClass->where('status', 1)->where('del_flag', 0)->where('group_id',$group)->column('id');
        return $data;
    }

    public static function getSearchUser($group=0){
        $UserClass=new self();

        if($group) {
            $search_user = $UserClass->where('group_id', $group)->where('status', 1)->where('del_flag', 0)->column('id');
        }else{
            $search_user=$UserClass->where('status', 1)->where('del_flag', 0)->column('id');;
        }
        return $search_user;
    }

    public static function getUserRoleList($search_param,$page_param){
        $UserClass=new self();

        if (is_numeric($search_param['id'])) {
            $where['u.id']=['=',$search_param['id']];
        }
        if (is_numeric($search_param['group_id'])) {
            $where['u.group_id']=['=',$search_param['group_id']];
        }
        if (!empty($search_param['role_id'])) {
            $where['u.role']=['=',$search_param['role_id'][0]];
            if(isset($search_param['role_id'][1])){
                $where['u.role_detail_id']=['=',$search_param['role_id'][1]];
            }
        }
        if (!empty($search_param['search_key']) && !empty($search_param['search_value'])) {
            $where['u.'.$search_param['search_key']]=['like','%'.$search_param['search_value'].'%'];
        }
        $where['u.del_flag']=['=',0];
        $where['u.status']=['=',1];
        $obj=$UserClass->alias('u')
            ->where($where)
            ->join('__ROLE__ r','u.role=r.id','left')
            ->join('role_detail rd','u.role_detail_id=rd.id','left')
            ->join('__GROUP__ g','u.group_id=g.id','left')
            ->field('u.id,u.user_name,u.status,u.role,u.role_detail_id,u.group_id,g.group_name,r.role_name,rd.role_name role_detail_name')
            ->paginate($page_param['page_size'],false,['page'=>$page_param['page_num']]);

        if($obj->isEmpty()){
            return ['status'=>true,'result'=>['data'=>[],'total'=>0]];
        }
        $count=$obj->total();
        $data=$obj->items();

        return ['status'=>true,'result'=>['data'=>$data,'total'=>$count]];
    }

    public static function createUser($data)
    {
        $UserClass=new self();
        $user_data=$UserClass->where('user_name',$data['user_name'])->where('del_flag',0)->field('id')->find();
        if(!empty($user_data)){
            return ['status'=>false,'result'=>'用户名已被占用'];
        }
        try {
            $result = $UserClass->insert($data);
            if ($result===false){
                return ['status'=>false,'result'=>'创建失败'];
            }
            return ['status'=>true,'result'=>'创建成功'];
        }catch (Exception $e){
            trace('createUser '.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'创建失败'];
        }
    }

    public static function getUserById($id)
    {
        $UserClass=new self();
        $data=$UserClass->where('id',$id)->where('del_flag',0)->field('user_name,role,role_detail_id,group_id')->find();
        return $data->toArray();
    }

    public static function updateUser($data)
    {
        $UserClass=new self();
        $user_data=$UserClass->where('user_name',$data['user_name'])->where('id','<>',$data['id'])->where('del_flag',0)->field('id')->find();
        if(!empty($user_data)){
            return ['status'=>false,'result'=>'用户名已被占用'];
        }
        try {
            $result = $UserClass->save($data,['id'=>$data['id']]);
            if ($result===false){
                return ['status'=>false,'result'=>'更新失败'];
            }
            return ['status'=>true,'result'=>'更新成功'];
        }catch (Exception $e){
            trace('updateUser '.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'更新失败'];
        }

    }
}